
/**************************************************************************
 * @doc rcptcmpt
 * @module rcptcmpt.C
 * Source for the Receipt Segment Module.
 *
 * Product     :   SoftPay 2000 (Global Payment Methodology).
 * <nl>Developer   :   Bruce Girdlestone
 * <nl>Notes       :
 *
 * @head3 RCPTCMPT Functions |
 * @index | RCPTCMPT
 * @end
 *
 * Copyright (c) 1996-2000 by VeriFone Inc. All rights reserved.
 *
 * No part of this software may be used, stored, compiled, reproduced,
 * modified, transcribed, translated, transmitted, or transferred, in any form
 * or by any means whether electronic, mechanical, magnetic, optical,
 * or otherwise, without the express prior written permission of VeriFone, Inc.
 *
 * Revision History
 * ------------------------------------------------------------------------
 * $log: \softpay\SPK2SIM\SOURCE\rcptcmpt.c $
 * ------------------------------------------------------------------------
 ********************************************************************************/



#include <stdio.h>
#include <string.h>
#include <VarRec.h>

#ifdef SDS
#include <svc.h>
#endif

#include <project.h>
#include <define.h>
#include <table.h>
#include <transact.h>
#include <funcs.h>
#include <transact.h>
#include <hdt.h>


#ifdef FORMS_MODULE 

#include <rcptcmpt.h>
#include <rcptsegs.h>
#include <codesec.h>
#include <pcounter.h>
#include <confio.h>

/* This buffer is initialised into a variable record and will be used
as a multiple index into the printer forms file */
unsigned char RECEIPT_FORM_FLEXI[MAX_RECEIPT_FORMS_FLEXI_SIZE];



/* NB ---- REMOVE :: Added for debug purposes
TRANSACTION_OBJECT tranObject1;
int inPrintLine(TRANSACTION_OBJECT * tranObject)
{
    int inError = 0;
    printf("Line printed \n");
    return inError;
}
int inPrintLine1(TRANSACTION_OBJECT * tranObject)
{
    int inError = 0;
    printf("Line printed \n");
    return inError;
}
*** DEBUG END ****/


/*****************************************************************************
* @func int | inInitFormFunctions |
*       Adds the default print segment functions into the Run Engine
*       Flexi-Record.  Here, we also create a FLEXI-RECORD that as used as
*       index into the receipt forms table RCPTCMPT.DAT.
*
* @rdesc return values.
* @flag int    |  Error value. Non-Zero indicates an error has occured.
* @end
*****************************************************************************/

int inAddFormFunctions() {
    RECEIPT_FORM_struc srReceiptSegment;
    short shError = 0;
    short inDone = 0;
    int inIndex;
    RECEIPT_FORM_REC_struc srRecieptForm;



    /* Initialise the variable record which will be used as a multiple index
    into the printer forms file */
    vVarInitRecord(RECEIPT_FORM_FLEXI, MAX_RECEIPT_FORMS_FLEXI_SIZE, 0);


    /* We have to have a duel index into the recipt forms table - RCPTCMPT.DAT
    * The formIdentifier, and the host Index, or Host name act as secondary keys */
    for (inIndex = 0; (!shError) && (!inDone) && (inIndex < MAX_FORM_RECORDS); inIndex ++) {
        /* Load up the associated record from the forms table */
        shError = inLoadConfRec(RECEIPT_FORMS_TABLE, sizeof(RECEIPT_FORM_REC_struc), inIndex, (char *) &srRecieptForm);
        if (shError == VS_SUCCESS) {
            /* Set up secondary key data */
            srReceiptSegment.inBatchHostIndex = srRecieptForm.inBatchHostIndex;
            strcpy((char *) srReceiptSegment.szHostName, (char *) srRecieptForm.szHostName);
            srReceiptSegment.inRecNum = inIndex;

            /* Now, add this index data to the variable record, using the
                    * formIdentifier as the main key
                    */
            shError = shVarAddField(RECEIPT_FORM_FLEXI, srRecieptForm.inFormIdentifier, (unsigned char *) &srReceiptSegment, sizeof(RECEIPT_FORM_struc));
        } else {
            inDone = 1;
            shError = 0;
        }//if
    }//while

    /******************************************************
    * ADD DEFAULT PRINTER FORM FUNCTION POINTERS HERE     *
    *******************************************************/


    /* Following here for debug purposes */
    /** DEBUG
    shError = inAddFunctionPointer(SEG_LINE_FEED, inPrintLine);
    shError = inAddFunctionPointer(SEG_FORM_FEED, inPrintLine1);
    tranObject1.srBRec.inCDTIndex =  SEG_HOST_ID0;
    inRunFormSegments(&tranObject1,SEG_PRINT_REPORT);
    tranObject1.srBRec.inCDTIndex =  SEG_HOST_ID1;
    inRunFormSegments(&tranObject1,SEG_PRINT_REPORT);
    tranObject1.srBRec.inCDTIndex =  SEG_HOST_ID1;
    inRunFormSegments(&tranObject1,SEG_PRINT_RECEIPT);
    tranObject1.srBRec.inCDTIndex =  0;
    inRunFormSegments(&tranObject1,SEG_TEST_REPORT);
    DEBUG COMPLETE */





    return shError;
}

static char ifOddBoundary(unsigned char* buff) {
    return (char) (((long) buff) % 2);
}

static char if4ByteBoundary(unsigned char* buff) {
    return (char) (((long) buff) % 4);
}


/*****************************************************************************
* @func int | inRunFormSegments |
*       This function is called to run the set of functions associated with a
*       specific Form.
* @parm TRANSACTION_OBJECT | tranObject |
*       The current transaction data block.
* @parm int | inFormIdentifier |
*       This identifies the Form record within RCPTCMPT.DAT
*
* @rdesc return values.
* @flag int    |  Error value. Non-Zero indicates an error has occured.
* @end
*****************************************************************************/

int inRunFormSegments(TRANSACTION_OBJECT* pobTran,int inFormIdentifier) {
#ifdef  COUNTER_DEFINED
    COUNTER_OBJECT obCounter;
#endif  /* COUNTER_DEFINED */

    /* This is the numeric ID of the current HOST being used */
    int inCurrentHostIndex = shGetHostGroupRef();
    RECEIPT_FORM_struc srReceiptSegment;
 //jrd one source
    #ifdef __arm
    unsigned short usFieldDataLength;
    #endif
    #ifdef _TARG_68000
    unsigned int usFieldDataLength;
    #endif	
    
    int  shError = 0;
    short shFound = 0;
    RECEIPT_FORM_REC_struc srRecieptForm;
    //  HDT_REC srCurrentHostRecord;
    unsigned char ucCurrentHostRecordBuff[SIZE_HDT_REC + 1];
    unsigned char * ucCurrentHostRecord = ucCurrentHostRecordBuff;

#ifdef __arm
     {
        char chBound;
        if ((chBound = if4ByteBoundary(ucCurrentHostRecord)) != 0)   /* if on odd boundary increment pointer */
            ucCurrentHostRecord += (4 - chBound);
    }  
#endif  
#ifdef _TARG_68000
    if (ifOddBoundary(ucCurrentHostRecord))   /* if on odd boundary increment pointer */
        ucCurrentHostRecord++;     
#endif

    /* Note :: We use a duel key for accessing a single record in the
    receipt component table. */

    /* All print Segment type indexes are loaded in this variable record */
    shError = shVarGetField(RECEIPT_FORM_FLEXI, inFormIdentifier, (unsigned char *) &srReceiptSegment, (sizeof(RECEIPT_FORM_struc)), &usFieldDataLength);

    /* IF there is an error at this stage, then this form type is NOT SUPPORTED */
    while ((!shError) && (!shFound)) {
        if (srReceiptSegment.inBatchHostIndex == inCurrentHostIndex) {
            /* We have found the matching Form record */
            shFound = TRUE;
        } else {
            /* Check if the host index is flagged for other search conditions */
            switch (srReceiptSegment.inBatchHostIndex) {
            case (short)SEG_SEARCH_CATCH_ALL:
                /* The record has been set as a catch all */
                shFound = TRUE;
                break;

            case (short)SEG_SEARCH_USE_HOST_NAME:
                /* The recrd has been set to use the host name as search key */
                //shError = inLoadHostData (inCurrentHostIndex);
                /* Load up the associated record from the forms table */
                shError = inLoadHDTRecFromStruct(inCurrentHostIndex, ucCurrentHostRecord);
                if (!shError) {
                    if (!strcmp(szGetHostFileNameFromStruct(ucCurrentHostRecord), (char *) srReceiptSegment.szHostName))
                        shFound = TRUE;
                    else {
                        /* Read in the next field with a mathcing field ID - formIdentifier */
                        /* Get the NEXT matching untyped field from a variable record */
                        shError = shVarGetNextField(RECEIPT_FORM_FLEXI, inFormIdentifier, (unsigned char *) &srReceiptSegment, (sizeof(RECEIPT_FORM_struc)), &usFieldDataLength);
                    }//if
                }
                break;

            default:
                /* Read in the next field with a mathcing field ID - formIdentifier */
                /* Get the NEXT matching untyped field from a variable record */
                shError = shVarGetNextField(RECEIPT_FORM_FLEXI, inFormIdentifier, (unsigned char *) &srReceiptSegment, (sizeof(RECEIPT_FORM_struc)), &usFieldDataLength);
                break;
            }//switch
        }//if
    }//while


    /* If a matching record was found, then run the associated form functions */
    if ((shFound) && (!shError)) {
        /* Load up the associated record from the forms table */
        shError = inLoadConfRec(RECEIPT_FORMS_TABLE, sizeof(RECEIPT_FORM_REC_struc), srReceiptSegment.inRecNum, (char *) &srRecieptForm);

        if (!shError) {
            /* NOW, Run all the functions associated with the selected form */
            shError = inRunEngine((short *) &(srRecieptForm.inSegmentIds[0]), (void *) pobTran);
#ifdef COUNTER_DEFINED
            switch (inFormIdentifier) {
            case    BASE_RECEIPT :
            case REPRINT_RECEIPT :
            case   SETTLE_REPORT :
            case  REPRINT_SETTLE :
                obCounter.inType = ROCS_PRINTED;
                obCounter.inIndex = inGetCounterIndex();    inIncCounter(&obCounter, 1);
                break;
            }
#endif  /* COUNTER_DEFINED */
        }//if
    }//if found



    return shError;
}

int RCPTCMPT_CODE_CHECK(char* a,char* b,short* c,char* d,unsigned char* e) {
    GET_VER(a, b, c, d, e);
    return 1;
}



#endif
